package com.mcintyret.site.journal;

import com.mcintyret.site.users.OwnedByUser;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

import javax.persistence.*;
import java.util.LinkedList;
import java.util.List;

/**
 * User: mcintyret2
 * Date: 28/03/2012
 */

@Entity
@Table(name = "journals")
public class Journal implements OwnedByUser {

    private Integer id;
    private String name;
    private JournalFamily journalFamily;
    private String description;
    private String username;
    private int order;
    private List<Timeseries> timeserieses = new LinkedList<Timeseries>();
    private List<TextEntry> textEntries = new LinkedList<TextEntry>();

    // For use by Hibernate
    Journal() {}

    public Journal(String name) {
        this(name, null);
    }

    public Journal(String name, String description) {
        setName(name);
        setDescription(description);
    }

    @Id
    @GeneratedValue
    @Column(name = "journal_id")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name = "username")
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


    @Column(name = "description")
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Column(name = "order_num")
    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

    public Journal addTextEntry(TextEntry textEntry) {
        textEntries.add(textEntry);
        textEntry.setJournal(this);
        textEntry.setUsername(username);
        return this;
    }

    public void setTextEntries(List<TextEntry> textEntries) {
        this.textEntries = textEntries;
    }


    @OneToMany(orphanRemoval = true, mappedBy = "journal")
    @OrderBy("time")
    @Cascade(CascadeType.ALL)
    public List<TextEntry> getTextEntries() {
        return textEntries;
    }

    public Journal addTimeseries(Timeseries timeseries) {
        timeserieses.add(timeseries);
        timeseries.setJournal(this);
        timeseries.setOrder(timeserieses.size());
        timeseries.setUsername(username);
        return this;
    }

    public void setTimeserieses(List<Timeseries> timeserieses) {
        this.timeserieses = timeserieses;
    }


    @OneToMany(orphanRemoval = true, mappedBy = "journal")
    @OrderBy("order")
    @Cascade(CascadeType.ALL)
    public List<Timeseries> getTimeserieses() {
        return timeserieses;
    }

    @ManyToOne(optional = false)
    @JoinColumn(name = "journal_family_id")
    public JournalFamily getJournalFamily() {
        return journalFamily;
    }

    public void setJournalFamily(JournalFamily journalFamily) {
        this.journalFamily = journalFamily;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Journal journal = (Journal) o;

        if (order != journal.order) {
            return false;
        }
        if (description != null ? !description.equals(journal.description) : journal.description != null) {
            return false;
        }
        if (journalFamily != null ? !journalFamily.equals(journal.journalFamily) : journal.journalFamily != null) {
            return false;
        }
        if (name != null ? !name.equals(journal.name) : journal.name != null) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (journalFamily != null ? journalFamily.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + order;
        return result;
    }

    public static class CompoundId {


    }
}
